package com.wenlong.train_ticketing_system_springboot.querymodule.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenlong.train_ticketing_system_springboot.querymodule.pojo.Ticket;
import com.wenlong.train_ticketing_system_springboot.querymodule.service.TicketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author 苏文龙
 * @version 1.0
 */
@RestController
@RequestMapping("/trainT")
@Slf4j
public class TicketController {
    @Autowired
    private TicketService ticketService;

    @RequestMapping("/getTicketsByPages")
    public Page<Ticket> getTicketsByPages(@RequestParam(value = "cur", defaultValue = "1") Long cur,
                                          @RequestParam(value = "descrip", required = false) String descrip,
                                          @RequestParam(value = "price", required = false) String price,
                                          @RequestParam(value = "store", required = false) String store,
                                          @RequestParam(value = "departTime", required = false) String departTime) {
        Page<Ticket> tickets = null;
        if (descrip != null || price != null || store != null || departTime != null) {
            tickets = ticketService.getTicketWithConditionByPages(cur, 4L, descrip, price, store, departTime);
            return tickets;
        }
        tickets = ticketService.getTicketByPages(cur, 4L);
        return tickets;
    }

    @RequestMapping("/handleTickets") // 考虑队列
    public Page<Ticket> handleTickets(@RequestParam(value = "cur", defaultValue = "1") Long cur,
                                      @RequestParam(value = "id") Long id,
                                      HttpServletRequest request) {
        Long uId = (Long) request.getSession().getAttribute("userId");
        Page<Ticket> ticketPage = null;
        try {
            ticketPage = ticketService.createOrderWithTransactionIntoRedis(cur, 4L, id, uId);
        } catch (Exception e) {
            ticketPage = getTicketsByPages(cur, null, null, null, null);
            log.info("there have some error:{}" + e.getMessage());
//            e.printStackTrace();
        }
        return ticketPage;
    }

    @RequestMapping("/createOrder") // 直接生成订单, 不考虑队列
    public Page<Ticket> createOrder(@RequestParam(value = "cur", defaultValue = "1") Long cur,
                                    @RequestParam(value = "id") Long id,
                                    HttpServletRequest request) {
        Long uId = (Long) request.getSession().getAttribute("userId");
        Page<Ticket> ticketPage = null;
        try {
            ticketPage = ticketService.createOrderIntoMysql(cur, 4L, id, uId);
        } catch (Exception e) {
            ticketPage = getTicketsByPages(cur, null, null, null, null);
            e.printStackTrace();
        }
        return ticketPage;
    }
}
